package com.flycms.web.system.rest;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.flycms.constant.StatusConst;
import com.flycms.core.base.BaseController;
import com.flycms.core.entity.PagingParam;
import com.flycms.core.entity.RestDataVo;
import com.flycms.core.utils.web.utils.CommonUtils;
import com.flycms.module.comment.model.Comments;
import com.flycms.module.comment.model.FlyContentType;
import com.flycms.module.comment.service.CommentsService;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年4月11日
 * @Date 2020年4月11日
 */

@RestController
@RequestMapping("/system/cm")
public class AdminCommentController extends BaseController {

private static final Logger logger = LogManager.getLogger(AdminCommentController.class);
    
    @Autowired
    protected CommentsService commentsService;
    
    /**
     * 获取评论
     * @param contentId
     * @param contentType
     * @param pageIndex
     * @param pageSize
     * @return
     * @author 沁言訫語
     */
    
    @GetMapping(value = {"/list/{contentType}", "/list"})
    public RestDataVo getCommentList(
            @PathVariable(value = "contentType", required = false) String contentType,
            @RequestParam(value = "contentId", required = false) Long contentId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        RestDataVo rest = null;
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        status = CommonUtils.validStatus(status);
        FlyContentType ct = CommonUtils.getContentType(contentType);
        if (ct == null) {
            ct = FlyContentType.ARTICLE;
        }
        try {
            List<Comments> commentList = commentsService.getCommentList(contentId, null, status,
                    paging.getOffset(), paging.getRows(), ct);
            int count = commentsService.getCommentCount(contentId, null, status, ct);
            rest = RestDataVo.PAGE(commentList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取评论异常");
        }
        return rest;
    }
    
    @PostMapping(value = "/{id}/{contentType}/remove")
    public RestDataVo removeComment(@PathVariable("id") Integer id,
            @PathVariable("contentType") String contentType) {
        RestDataVo rest = null;
        FlyContentType ct = CommonUtils.getContentType(contentType);
        try {
            Comments cm = commentsService.queryCommentById(id, ct);
            if (cm == null || Objects.equals(cm.getStatus(), StatusConst.DELETE.value())) {
                commentsService.removeComment(id, ct);
                rest = RestDataVo.OK("删除评论成功");
            } else {
                commentsService.editComment(id, null, StatusConst.DELETE.value(), new Date(), ct);
                rest = RestDataVo.OK("改变状态评论为删除成功");
            }
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("删除评论异常");
        }
        return rest;
    }
    
    @PostMapping(value = "/{id}/{contentType}/lock")
    public RestDataVo lockComment(@PathVariable("id") Integer id,
            @PathVariable("contentType") String contentType) {
        RestDataVo rest = null;
        FlyContentType ct = CommonUtils.getContentType(contentType);
        try {
            commentsService.editComment(id, null, StatusConst.LOCKED.value(), new Date(), ct);
            rest = RestDataVo.OK("锁定评论成功");
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("锁定评论异常");
        }
        return rest;
    }
    
}
